home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / WLIST.ARJ / LINKLIST.DOC < prev    next >
Text File  |  1992-05-16  |  10KB  |  352 lines

  1. Linked List Library Documentation
  2.  
  3. February 22, 1991
  4. May 15, 1992 (minor tweaks)
  5.  
  6. by Paul Wheaton
  7.  
  8. To make these libraries available, you need to include <LinkList.h>
  9. and link with LinkList.obj.
  10.  
  11. 1    what is a linked list:  help for the heapless
  12. 1.1    some references
  13. 1.2    my version
  14. 2    LinkedList class
  15. 2.01    Root()
  16. 2.02    Cur()
  17. 2.03    Size()
  18. 2.04    Top()
  19. 2.05    Num()
  20. 2.06    Next()
  21. 2.07    Prev()
  22. 2.08    Last()
  23. 2.09    Insert()
  24. 2.10    Add()
  25. 2.11    Append()
  26. 2.12    Push()
  27. 2.13    DelCur()
  28. 2.14    DelCurObj()
  29. 2.15    Pop()
  30. 2.16    operator[]
  31. 3    custom linked list classes
  32. 4    Stack class
  33. 5    Queue class
  34.  
  35.  
  36.  
  37. 1 what is a linked list:  help for the heapless
  38.  
  39. 1.1 some references
  40.  
  41.   Perhaps the best way to learn about linked lists is to go one of the
  42.   foremost authorities:  In the book "Algorithms + Data Structures =
  43.   Programs" by Niklaus Wirth (1976 Prentice-Hall) in the chapter
  44.   "Dynamic Information Structures" under the section "Linear Lists" is
  45.   a complete tutorial.
  46.  
  47.   The book "Structure and Interpretation of Computer Programs" by
  48.   Abelson, Sussman and Sussman (1985 MIT Press) is based on the Scheme
  49.   language (a LISP dialect) so it's just thick with list stuff.
  50.   Section 2.2.1 should give you a good idea of what's going on here.
  51.  
  52.   The book "An Introduction to Object Oriented Programming and C++" by
  53.   Wiener and Pinson (1988 Addison-Wesley) has the only decent C or C++
  54.   linked list reference I could find inside half an hour.  See section
  55.   6.2: "An Object-Oriented Solution to Generating a Linked List"
  56.  
  57. 1.2 my version
  58.  
  59.   I'm going to assume that you have a pretty good grip on what a heap
  60.   is:  that dynamic memory thing.
  61.  
  62.   Let's say that you have a structure called a DooDad that takes up
  63.   about a thousand bytes.  Let's also say that you need to keep about
  64.   a hundred of them, ordered, in memory for the sake of speed.  Part
  65.   of what the user will be doing with this list is changing the order
  66.   of it.
  67.  
  68.   If you wanted to do a malloc() to store all of this stuff, you'd
  69.   have to ask for about 100,000 bytes.  More than Microsoft C can
  70.   handle in one malloc.  Even if you could get that much memory,
  71.   what's going to happen when you try to move a DooDad from the middle
  72.   and put it at the beginning?  A whole lot of time consuming memory
  73.   copying, that's what (e.g. make a 1000 byte temp copy of element 50;
  74.   shift about 50,000 bytes about 1000 bytes to the right; copy your
  75.   temp to the beginning of your data area).  If you had to insert and
  76.   delete DooDads a lot, things could be unbearably slow.
  77.  
  78.   Now for a linked list approach.  With a tiny struct
  79.  
  80.     struct Node
  81.       {
  82.         Node* PrevNode;
  83.         DooDad* Dat;
  84.         Node* NextNode;
  85.       };
  86.  
  87.   and by allocating memory for your DooDads in thousand byte chunks,
  88.   you can create a list like this:
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.       xxx picture goes here
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.   To move a DooDad from the middle and put it at the beginning, change
  111.   six pointers.
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.       xxx picture goes here
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.   Voila'!  Everything just the way you want it and without all that
  134.   copying stuff.  The second diagram might look a little gross, but
  135.   inside of the computer it looks just as nice as the first one.  If
  136.   you want, the second diagram can be redrawn to look just like the
  137.   first.
  138.  
  139.   Linked lists are also good for bunches of different sized objects.
  140.  
  141. 2 LinkedList class
  142.  
  143.   This linked list class currently uses the model described above.
  144.   Sometimes called a "doubly linked list with nodes", the "doubly"
  145.   referring to the fact that the list has "previous" pointers as well
  146.   as "next" pointers.  The "nodes" part refers to the pointers being
  147.   in its own record and not part of the data record.
  148.  
  149.   There is only one way to create a LinkedList object.  With
  150.   absolutely no parameters (e.g. "LinkedList L;").  When you add any
  151.   data to the list, you must create your data yourself and simply pass
  152.   in a pointer to your data.  You are welcome to add data that is on
  153.   the heap, the stack (local variables or parameters) or the data
  154.   segment (global data).  If you want to add the same object several
  155.   times, you'll just have several nodes pointing to the same place.
  156.  
  157.   When the list is deleted or goes out of scope, it will delete any
  158.   nodes that may still be active.  It will not delete any data that
  159.   you have connected to the list.
  160.  
  161.   All of the pointers are of type "void*".
  162.  
  163. 2.01 Root()
  164.  
  165.   Prototype:  void* Root()
  166.  
  167.   The "current pointer" is changed to point to the last element in the
  168.   list and then the new "current pointer" is returned. Returns NULL if
  169.   there are no elements.
  170.  
  171. 2.02 Cur()
  172.  
  173.   Prototype:  void* Cur()
  174.  
  175.   Returns a pointer to the current element.  Returns NULL if there
  176.   are no elements.
  177.  
  178. 2.03 Size()
  179.  
  180.   Prototype:  Long Size()
  181.  
  182.   Returns the number of elements in the list.
  183.  
  184. 2.04 Top()
  185.  
  186.   Prototype:  Long Top()
  187.  
  188.   Returns the index to the last element.  Equivalent to Size()-1.
  189.  
  190. 2.05 Num()
  191.  
  192.   Prototype:  Long Num()
  193.  
  194.   Returns the index to the current element.  Would be any number from
  195.   0 to Top().
  196.  
  197. 2.06 Next()
  198.  
  199.   Prototype:  void* Next()
  200.  
  201.   The Cur() pointer is changed to point to the next element in the
  202.   list and then the new Cur() is returned.  Returns NULL if there are
  203.   no elements.
  204.  
  205. 2.07 Prev()
  206.  
  207.   Prototype:  void* Prev()
  208.  
  209.   The Cur() pointer is changed to point to the previous element in the
  210.   list and then the new Cur() is returned.  Returns NULL if there are
  211.   no elements.
  212.  
  213. 2.08 Last()
  214.  
  215.   Prototype:  void* Last()
  216.  
  217.   The Cur() pointer is changed to point to the last element in the
  218.   list and then the new Cur() is returned.  Returns NULL if there are
  219.   no elements.
  220.  
  221. 2.09 Insert()
  222.  
  223.   Prototype:  Bool Insert(void* NewRec)
  224.  
  225.   Will insert a new node between the previous element and the current
  226.   element.  On exit, Cur() will point to NewRec.  Returns False if
  227.   there was not enough memory to create a new node.
  228.  
  229. 2.10 Add()
  230.  
  231.   Prototype:  Bool Add(void* NewRec)
  232.  
  233.   Will insert a new node between the current element and the next
  234.   element.  On exit, Cur() will point to NewRec.  Returns False if
  235.   there was not enough memory to create a new node.
  236.  
  237. 2.11 Append()
  238.  
  239.   Prototype:  Bool Append(void* NewRec)
  240.  
  241.   Will stick a new node on the end of the list. On exit, Cur() will
  242.   point to NewRec.  Returns False if there was not enough memory to
  243.   create a new node.
  244.  
  245. 2.12 Push()
  246.  
  247.   Prototype:  Bool Push(void* NewRec)
  248.  
  249.   Will stick a new node on the end of the list. On exit, Cur() will
  250.   point to NewRec.  Returns False if there was not enough memory to
  251.   create a new node.
  252.  
  253.   Note that this works the same way as Append().  This function is
  254.   provided for clarity in your code when you wish to use a linked list
  255.   like a stack (see the Pop() function).
  256.  
  257. 2.13 DelCur()
  258.  
  259.   Prototype:  void DelCur()
  260.  
  261.   I recommend that if this node points to data on the heap (as is most
  262.   often the case) that you should first remove your data from the heap
  263.   using the Cur() pointer.
  264.  
  265.   This function will remove the current node from the list.  It will
  266.   *not* do anything to the data.  On exit, Cur() will point to what
  267.   would have been pointed to by Next().
  268.  
  269. 2.14 DelCurObj()
  270.  
  271.   Prototype:  void DelCurObj()
  272.  
  273.   This function will attempt to "delete" your data.  This will only
  274.   work on data that has been created with "new" and does not have a
  275.   destructor.  After this, it will remove the current node from the
  276.   list. On exit, Cur() will point to what would have been pointed to
  277.   by Next().
  278.  
  279. 2.15 Pop()
  280.  
  281.   Prototype:  void* Pop()
  282.  
  283.   This function will delete the last node in the list and return a
  284.   pointer to what that node pointed to.  On exit Cur() will point to
  285.   the new last element.
  286.  
  287. 2.16 operator[]
  288.  
  289.   Prototype:  void* operator[](Long Index)
  290.  
  291.   This operator will allow your linked list to be treated like an
  292.   array.  If you want a pointer to the fifth element of your
  293.   LinkedList name "L", simply use "L[4]".  A valid function call would
  294.   be something like "memcpy(L[2],L[8],400);".  On exit, Cur() will
  295.   point to the same place.
  296.  
  297. 3 custom linked list classes
  298.  
  299.   A problem with the LinkedList class is that it will only deal with
  300.   void pointers.  That means that if you want to reference a field of
  301.   a struct in a LinkedList, you will first have to take the pointer it
  302.   gives you and cast it to be a pointer to your object and then
  303.   de-reference a field.  What a hassle!  For example:
  304.  
  305.     struct FooType
  306.       {
  307.         Char Name[40];
  308.         Long SSN;
  309.         Long BirthYear;
  310.       };
  311.  
  312.     void Bar()
  313.       {
  314.         LinkedList L;
  315.         L.Add(new FooType);
  316.         strcpy(((FooType*)L.Cur())->Name,"Bob");
  317.         ((FooType*)L.Cur())->SSN=123456789;
  318.         ((FooType*)L.Cur())->BirthYear=1954;
  319.       }
  320.  
  321.   Using the same FooType struct, we could have
  322.  
  323.     CreateLinkedListClass(FooList,FooType);
  324.  
  325.     void Bar()
  326.       {
  327.         FooList L;
  328.         L.Add(*new FooType);
  329.         strcpy(L.Cur().Name,"Bob");
  330.         L.Cur().SSN=123456789;
  331.         L.Cur().BirthYear=1954;
  332.       }
  333.  
  334.   Note that all the parameters in your custom class accept and return
  335.   references instead of pointers.
  336.  
  337. 4 Stack class
  338.  
  339.   The Stack class works exactly the same as the LinkedList class.
  340.   This class is provided for readability's sake only.  If you intend
  341.   to use your linked lisk like a stack, you might want to declare it
  342.   as "Stack FooStack;" rather than "LinkedList FooStack;"
  343.  
  344. 5 Queue class
  345.  
  346.   This class works exactly the same as the LinkedList class *except*
  347.   for the Pop() function.  Pop() will delete the first node in the list
  348.   and return a pointer to what that node pointed to.  On exit Cur()
  349.   will point to the new first element.  So now you see how it behaves
  350.   like a Queue instead of a Stack:  You Push() stuff onto the top and
  351.   Pop() them off the bottom.
  352.